package com.dingreading.cloud.soms.controller;

import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.KitUtil;
import com.dingreading.cloud.common.util.TimeUtil;
import com.dingreading.cloud.common.util.aliyun.OSSClientUtil;
import com.dingreading.cloud.soms.entity.CaCallNameImg;
import com.dingreading.cloud.soms.service.CaCallNameImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/front/caCallNameImg")
@Api(tags = "门店-会员活动记录-作文图片")
public class FrontCaCallNameImgController {

    @Resource
    protected CaCallNameImgService callNameImgService;


    @ApiOperation(value = "获取点名记录的作文图片集合")
    @PostMapping(value = "/getImgList")
    public R<Object> getImgList(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "点名记录uid") @RequestParam(value = "callUid") String callUid
    ) {
        List<CaCallNameImg> list = callNameImgService.listByCallUid(storeUid, callUid);
        return R.ok(list);
    }


    @ApiOperation(value = "保存作文图片")
    @PostMapping(value = "/add")
    public R<Object> add(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "点名记录uid") @RequestParam(value = "callUid") String callUid,
            @ApiParam(value = "图片地址") @RequestParam(value = "url", required = false) String url
    ) {
        CaCallNameImg data = new CaCallNameImg();
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setCallUid(callUid);
        data.setUrl(url);
        data.setUid(KitUtil.getUuid());
        callNameImgService.saveOrUpdate(data);
        return R.ok();
    }

    @ApiOperation(value = "删除作文图片")
    @PostMapping(value = "/delete")
    public R<Object> delete(
            @ApiParam(value = "公司uid") @RequestParam(value = "corpUid") String corpUid,
            @ApiParam(value = "门店uid") @RequestParam(value = "storeUid") String storeUid,
            @ApiParam(value = "门店名称") @RequestParam(value = "storeName") String storeName,
            @ApiParam(value = "员工uid") @RequestParam(value = "staffUid") String staffUid,
            @ApiParam(value = "员工名") @RequestParam(value = "staffName") String staffName,
            @ApiParam(value = "图片uid") @RequestParam(value = "uid") String uid
    ) {
        CaCallNameImg data = callNameImgService.getByUid(storeUid, uid);
        if (data == null)
            return R.failGet();
        if (TimeUtil.moreThanDay(data.getAddTime(), 7)) {
            return R.fail("录入超过7天，不能操作");
        }
        int i = callNameImgService.deleteImg(storeUid, uid);
        if (i <= 0)
            return R.failDel();
        deleteOssFile(data.getUrl());
        return R.ok();
    }

    /**
     * 删除阿里云图片
     */
    private void deleteOssFile(String url) {
        OSSClientUtil ossClientUtil = new OSSClientUtil();
        ossClientUtil.initOss();
        ossClientUtil.deleteFile(OSSClientUtil.publicBucket, url);
    }

}